package pub.tbc.atps.util;

import pub.tbc.atps.config.PropConfig;
import pub.tbc.toolkit.Objs;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author tbc on 2016/11/29 16:51:14.
 */
public class RedisHelper {
    //    @Value("${redis.host:127.0.0.1}")
    private static String host = PropConfig.get("redis.host");
    //    @Value("${redis.port:6379}")
    private static int port = PropConfig.getInt("redis.port");

    private static JedisPool pool = new JedisPool(new JedisPoolConfig(), host, port, 0);

    private static Jedis client = new Jedis(host, port);

    public static void returnPool(Jedis client) {
        if (Objs.nonEmpty(client)) {
            //新版本jedis可以jedis.close()将连接归还连接池，旧版本不能这样还->pool.return...
            client.close();
        }
    }

    public static Jedis getClientFromPool() {
        return pool.getResource();
    }

    public static Jedis getClient() {
        return client;
    }


}
